#region legal please read
/*
 * Copyright iFinity Software 2012 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * */
#endregion

using System;
using System.Runtime.Serialization;
using DotNetNuke.Common.Utilities;
namespace DotNetNuke.Modules.SocialDashboard.Components
{
    public class SocialDashboardInfo
    {
        

        /// <summary>
        /// Constructor
        /// </summary>
        public SocialDashboardInfo()
        {
        }


        #region properties

        public int PortalId { get; set; }
        public string PortalName { get; set; }
        public int NumberUsers { get; set; }
        public int NumberGroups { get; set; }
        public int UsersInGroups { get; set; }
        public int UsersWithFriends { get; set; }
        public int NumberFriendships { get; set; }
        public DateTime FirstFriendshipDate { get; set; }
        public DateTime LastFriendshipDate { get; set; }
        public string FriendshipName { get; set; }
        public int UsersWithFollowers { get; set; }
        public int UsersFollowing { get; set; }
        public int NumberFollowers { get; set; }
        public DateTime FirstFollowerDate { get; set; }
        public DateTime LastFollowerDate { get; set; }
        public string FollowingName { get; set; }

        public string DispNumberUsers
        {
            get
            {
                return CheckAndReturnString(NumberUsers);
}
        }
        public string DispNumberGroups
        {
            get
            {
                return CheckAndReturnString(NumberGroups);
            }
        }
        public string DispUsersInGroups
        {
            get
            {
                return CheckAndReturnString(UsersInGroups, NumberUsers);
            }
        }
        public string DispUsersWithFriends
        {
            get
            {
                return CheckAndReturnString(UsersWithFriends, NumberUsers);
            }
        }
        public string DispNumberFriendships
        {
            get
            {
                return CheckAndReturnString(NumberFriendships, FriendshipName, NumberUsers);
            }
        }
        public string DispFirstFriendshipDate
        {
            get
            {
                return CheckAndReturnString(FirstFriendshipDate);
            }
        }
        public string DispLastFriendshipDate
        {
            get
            {
                return CheckAndReturnString(LastFriendshipDate);
            }
        }
        public string DispUsersWithFollowers
        {
            get
            {
                return CheckAndReturnString(UsersWithFollowers, NumberUsers);
            }
        }
        public string DispUsersFollowing
        {
            get
            {
                return CheckAndReturnString(UsersFollowing, FollowingName, NumberUsers);
            }
        }
        public string DispNumberFollowers
        {
            get
            {
                return CheckAndReturnString(NumberFollowers, FollowingName);
            }
        }
        public string DispFirstFollowerDate
        {
            get
            {
                return CheckAndReturnString(FirstFollowerDate);
            }
        }
        public string DispLastFollowerDate
        {
            get
            {
                return CheckAndReturnString(LastFollowerDate);
            }
        }
        #endregion
        private string SafePct(int denominator, int numerator)
        {
            string pct = "0.00%";
            if (numerator > 0 && denominator > 0)
                pct = string.Format("{0:P2}", (decimal)numerator / denominator);
            return pct;
        }
        private string CheckAndReturnString(DateTime value)
        {
            if (DateTime.MinValue == value)
                return "None";
            else
                return value.ToShortDateString();
        }
        private string CheckAndReturnString(int value, string name, int numeratorForPct)
        {
            string result = CheckAndReturnString(value, name);
            if (value > Null.NullInteger)
                result += " [" + SafePct(numeratorForPct, value) + "]";
            return result;
        }
        private string CheckAndReturnString(int value, int numeratorForPct)
        {
            string result = CheckAndReturnString(value);
            if (value > Null.NullInteger)
                result += " [" + SafePct(numeratorForPct, value) + "]";
            return result;
        }
        private string CheckAndReturnString(int value)
        {
            return CheckAndReturnString(value, null);
        }
        private string CheckAndReturnString(int value, string name)
        {
            if (value > Null.NullInteger)
                if (name == null)
                    return value.ToString();
                else
                    return value.ToString() + " " + name;
            else
                return "None";
        }

        internal void WriteXml(System.Xml.XmlWriter writer)
        {
            //Write start of main element 
            writer.WriteStartElement("portalSocialData");
            writer.WriteElementString("PortalName", PortalName);
            writer.WriteElementString("NumberUsers", DispNumberUsers);
            writer.WriteElementString("UserWithFriends", DispUsersWithFriends);
            writer.WriteElementString("NumberFriendships", DispNumberFriendships);
            writer.WriteElementString("FirstFriendshipDate", DispFirstFriendshipDate);
            writer.WriteElementString("LastFriendshipDate", DispLastFriendshipDate);
            writer.WriteElementString("UsersWithFollowers", DispUsersWithFollowers);
            writer.WriteElementString("UsersFollowing", DispUsersFollowing);
            writer.WriteElementString("FirstFollowerDate", DispFirstFollowerDate);
            writer.WriteElementString("LastFollowerDate", DispLastFollowerDate);
            writer.WriteElementString("NumberGroups", DispNumberGroups);
            writer.WriteElementString("UsersInGroups", DispUsersInGroups);
            //Write end of social info
            writer.WriteEndElement();
        }
    }
}
